<?php
/* This file is part of Mirasol CMS
   (C) 2011 by Chris Alban Hansen.
   Released under the terms of the GNU General Public License.
   See COPYING in the top level directory of the Mirasol CMS installation. */

include "{$_SERVER['DOCUMENT_ROOT']}/includes/config.php";
include "{$_SERVER['DOCUMENT_ROOT']}/includes/db.php";
include "{$_SERVER['DOCUMENT_ROOT']}/includes/login.php";

if (empty ($login['username']))
  exit;
	
$connection = db_open ();

$doctype = isset ($_GET['dt']) ? $_GET['dt'] : 1;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:php="http://php.net/xsl" xml:lang="en">
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <meta name="robots" content="noindex, nofollow" />
    <title>Pick file</title>
		
		<link type="text/css" href="./styles/overcast/jquery-ui.css" rel="Stylesheet" />
    <script type="text/javascript" src="./scripts/jquery.min.js"></script>
    <script type="text/javascript" src="./scripts/jquery-ui.min.js"></script>
		<script type="text/javascript">
		  $(document).ready (function ()
			  {
					// Buttons
					$("input:button, input:submit", ".operate" ).button ();
				});
		</script>
		
		<script type="text/javascript">
		  //<![CDATA[
			  function selectImage (name, fid)
				{
<?php
if (isset ($_GET['field2']))
{
?>
					opener.document.getElementById ("<?=$_GET['field1'] ?>").value = name;
					opener.document.getElementById ("<?=$_GET['field2'] ?>").value = fid;
<?php
} /* isset ($_GET['field2']) */
else
{
?>
					opener.document.getElementById ("<?=$_GET['field1'] ?>").value = "<?=$app_websitepath ?>/file.php?fn="+name;
<?php
} /* !isset ($_GET['field2']) */
?>
					window.close ();
				}
			//]]>
		</script>

    <link rel="icon" href="./favicon.ico" type="image/x-icon" /> 
    <link rel="shortcut icon" href="./favicon.ico" type="image/x-icon" />
    <link rel="stylesheet" href="./styles/mirasol.css" type="text/css" media="screen" />
  </head>

  <body>
    <div id="dialog">
<?php
if (isset ($_GET['field2']))
{
?>
			<div id="doctypes">
				<h3>File types</h3>
<?php
asort ($doctypes);
foreach ($doctypes as $key => $type)
	print "&bull; <a href=\"pickfile.php?field1={$_GET['field1']}&amp;field2={$_GET['field2']}&amp;dt=$key\">$type</a><br />\n";
?>
			</div>
<?php
} /* isset ($_GET['field2']) */
?>
			
			<div id="browser">
				<table cellspacing="0" cellpadding="0" style="width: 100%">
<?php
$result = mysql_query ("SELECT id, filename, filename_orig FROM ".db_maketablename ($table_files)." WHERE doctype='$doctype' ORDER BY filename_orig");
if (mysql_num_rows ($result) > 0)
{
	$col = 1;
  while ($row = mysql_fetch_array ($result))
	  {
			if ($col == 1)
			  print "<tr>";
			
			$file = "$app_filespath/{$row['filename']}";
			if ($doctype == 1) /* Image */
				{
					list ($width, $height) = getimagesize ("{$_SERVER['DOCUMENT_ROOT']}$file");
          if ($width > 200)
            {
              $newwidth = 200;
              $difftoheight = (($width - $newwidth) * 100) / $width;
              $newheight = round ($height - ($height * $difftoheight / 100));
              
              $width = $newwidth;
              $height = $newheight;
              if ($height > 200)
                {
                  $newheight = 200;
                  $difftowidth = (($height - $newheight) * 100) / $height;
                  $newwidth = round ($width - ($width * $difftowidth / 100));
                }
            }
          else
            {
							$newheight = $height;
					    $newwidth = $width;
						}
					print "<td style=\"text-align: center; vertical-align: bottom;\">";
					print "<a href=\"javascript:selectImage('{$row['filename_orig']}',{$row['id']});\" title=\"Click to select\"><img src=\"$file\" style=\"border: none; height: ".$newheight."px; width: ".$newwidth."px;\" alt=\"\" /></a>";
					print "<br/><strong>{$row['filename_orig']}</strong></td>";
					$col++;
				}
			
			if ($col > 3)
			  {
			    print "</tr><tr><td colspan=\"3\">&nbsp;</td></tr>";
					$col = 1;
			  }
		}
	if ($col != 1)
	  print "</tr>";
}
mysql_free_result ($result);
?>
				</table>
			</div>
			
			<div class="clear"></div>
		</div>
  </body>
</html>
<?php
db_close ($connection);
?>
